﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using Game;
using Mekiasm.Common;
using Mekiasm.FluidElement;
using System.Linq;

namespace Mekiasm
{
    public class FluidTank : ComplexModelItem
    {
        public static int Index;

        public static ModelItem InnerBox;

        public long MaxAmount => GetMaxPower();

        public ComponentEnergyMachine.FuelType ChargeFuelType => ComponentEnergyMachine.FuelType.Fliud;

        public FluidTank() : base(new ModelItem[] {
            new ModelItem("PoleRF", "fluid_tank"), new ModelItem("PoleLB", "fluid_tank"), new ModelItem("Base", "fluid_tank"),
            new ModelItem("BackGlass", "guigaugestandard"), new ModelItem("LeftGlass", "guigaugestandard"), new ModelItem("FrontGlass", "guigaugestandard"),
            new ModelItem("PoleFL", "fluid_tank"), new ModelItem("Top", "fluid_tank"), new ModelItem("PoleBR", "fluid_tank"),
            new ModelItem("RightGlass", "guigaugestandard") ,new ModelItem("InnerBox","transparent")}, "fluid_tank", "基础流体储罐")
        {
            
            InnerBox = ModelItems.First(x=>x.meshName== "InnerBox");
        }
        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            for (int i = 0; i < ModelItems.Length; i++)
            {
                var model = ModelItems[i];
                if (model.Texture2D == null) continue;
                if (model.meshName == "InnerBox") continue;
                BlocksManager.DrawMeshBlock(primitivesRenderer, ModelItems[i].BlockMesh, ModelItems[i].Texture2D, Color.White, size * IconScale, ref matrix, environmentData);
            }
        }

        public override void GenerateTerrainVertices(Block block, BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            for (int i = 0; i < ModelItems.Length; i++)
            {
                var model = ModelItems[i];
                if (model.meshName == "InnerBox") continue;
                var g = geometry.GetGeometry(model.Texture2D);
                TerrainGeometrySubset s = null;
                if (model.meshName.EndsWith("Glass")) s = g.SubsetAlphaTest;
                if (model.meshName.StartsWith("Pole")) s = g.SubsetOpaque;
                else s = g.SubsetTransparent;
                generator.GenerateMeshVertices(block, x, y, z, model.BlockMesh, Color.White, null, s);
            }
        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Fluid;
        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }

        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            int id = componentMiner.ActiveBlockValue;
            id = Terrain.ExtractContents(id);
            if (BlocksManager.Blocks[id].IsPlaceable_(raycastResult.Value))
            {
                return false;
            }
            ComponentFluidTank mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentFluidTank>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new FluidTankWidget(mcomponent,componentMiner);
            return true;
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z,
                new List<ComponentEnergyMachine.Slot>() {
                    new ComponentEnergyMachine.Slot() { FuelCapacity = 102400 ,SlotType=ComponentEnergyMachine.SlotType.IN|ComponentEnergyMachine.SlotType.OUT|ComponentEnergyMachine.SlotType.Charge_SlotOut},
                },
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Fluid)
            }), new MachineAmountLimitConfig() { OutputFluidAmountLimit = 32, InputFluidAmountLimit = 32 });
            FluidSystem.Add(new FluidTankElement(x, y, z));
        }

        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            FluidSystem.Add(new FluidTankElement(x, y, z));
        }

        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            FluidSystem.Remove(new Point3(x, y, z));
            ILibrary.RemoveBlockEntity(x, y, z);
        }
    }
}
